package com.hd.dao.impl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.hd.service.BaseService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {

	private BaseService baseService;
	
	public void find(){
		baseService.find();
	}
	public Map<String,Object> getData(String sql)throws Exception{
		return baseService.getData(sql);
	}
	public HttpServletRequest getRequest(){
		HttpServletRequest request = ServletActionContext.getRequest();
		return request;
	}
	@SuppressWarnings("unchecked")
	protected <T>T getSession(String key){
		Map<String ,Object> map = ActionContext.getContext().getSession();
		return (T)map.get(key);
	}
	protected void responseToJsonObject(Object info){
		ajaxResponse( JSONObject.fromObject( info ));
	}
	protected void ajaxResponse(Object info){
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.write(info.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(out!=null){
				out.close();
			}
		}
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

}
